package com.allen.retrofit2_rxjava2.view.calendar.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.bean.CalendarData;
import com.allen.retrofit2_rxjava2.bean.Day;
import com.allen.retrofit2_rxjava2.view.calendar.view.ChooseDateView;
import com.allen.retrofit2_rxjava2.view.calendar.view.PinnedHeaderListView;
import com.ieyelf.service.util.Logger;


public class CustomAdapter extends BaseAdapter
        implements OnScrollListener, PinnedHeaderListView.PinnedHeaderAdapter {
    private static final String TAG = CustomAdapter.class.getSimpleName();
    private Context mContext;
    private List<CalendarData> mData;
    private LayoutInflater mLayoutInflater;
    private int firstDayOfWeek = -1;
    private String dayOfWeek[] = new String[]{"日", "一", "二", "三", "四", "五", "六"};
    private Day starttime = null;
    private Day endtime = null;
    private Day today = null;


    public CustomAdapter(Context pContext, List<CalendarData> pData) {
        mContext = pContext;
        mData = pData;

        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.listview_item, null);

            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.firstInWeekText = (TextView) convertView.findViewById(R.id.textview_week_first);
            viewHolder.secondInWeekText = (TextView) convertView.findViewById(R.id.textview_week_second);
            viewHolder.thirdInWeekText = (TextView) convertView.findViewById(R.id.textview_week_third);
            viewHolder.fourthInWeekText = (TextView) convertView.findViewById(R.id.textview_week_fourth);
            viewHolder.fifthInWeekText = (TextView) convertView.findViewById(R.id.textview_week_fifth);
            viewHolder.sixthInWeekText = (TextView) convertView.findViewById(R.id.textview_week_sixth);
            viewHolder.seventhInWeekText = (TextView) convertView.findViewById(R.id.textview_week_seventh);
            viewHolder.chooseDateView = (ChooseDateView) convertView.findViewById(R.id.chooseDateView);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        CalendarData data = (CalendarData) getItem(position);
        Calendar calendar = data.getCalendar();

        if (needTitle(position)) {
            viewHolder.title.setText(data.getTitle());
            viewHolder.title.setVisibility(View.VISIBLE);
        } else {
            viewHolder.title.setVisibility(View.GONE);
        }

        if (null != calendar) {
            firstDayOfWeek = calendar.getFirstDayOfWeek();
        }
        if (-1 != firstDayOfWeek &&
                viewHolder.firstInWeekText.getText().toString().isEmpty()) {
            if (null != viewHolder.firstInWeekText)
                viewHolder.firstInWeekText.setText(dayOfWeek[(firstDayOfWeek - 1) % 7]);
            if (null != viewHolder.secondInWeekText)
                viewHolder.secondInWeekText.setText(dayOfWeek[(firstDayOfWeek + 1 - 1) % 7]);
            if (null != viewHolder.thirdInWeekText)
                viewHolder.thirdInWeekText.setText(dayOfWeek[(firstDayOfWeek + 2 - 1) % 7]);
            if (null != viewHolder.fourthInWeekText)
                viewHolder.fourthInWeekText.setText(dayOfWeek[(firstDayOfWeek + 3 - 1) % 7]);
            if (null != viewHolder.fifthInWeekText)
                viewHolder.fifthInWeekText.setText(dayOfWeek[(firstDayOfWeek + 4 - 1) % 7]);
            if (null != viewHolder.sixthInWeekText)
                viewHolder.sixthInWeekText.setText(dayOfWeek[(firstDayOfWeek + 5 - 1) % 7]);
            if (null != viewHolder.seventhInWeekText)
                viewHolder.seventhInWeekText.setText(dayOfWeek[(firstDayOfWeek + 6 - 1) % 7]);
        }

        if (null != calendar) {
            viewHolder.chooseDateView.setCalendar((Calendar) calendar.clone());

        }
        if (position == 0) {
            Day endtime = new Day(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.DAY_OF_WEEK), false);
            today = endtime;
            viewHolder.chooseDateView.setToday(today);
        }

        viewHolder.chooseDateView.setOnDaySelectedListener(new ChooseDateView.OnDaySelectedListener() {
            @Override
            public void onClick(int index) {
                if (-1 != index && null != viewHolder.chooseDateView && null != viewHolder.chooseDateView.getDayList() &&
                        viewHolder.chooseDateView.getDayList().size() > 0 &&
                        viewHolder.chooseDateView.getDayList().size() > index) {
                    starttime = viewHolder.chooseDateView.getDayList().get(index);
                    if (null != starttime) {
                        if (!viewHolder.chooseDateView.isValid(starttime, viewHolder.chooseDateView.getDayList().get(index)))
                            if (null != chooseDataListenter) {
                                chooseDataListenter.error(0);
                                return;
                            }

                    }
                    if (viewHolder.chooseDateView.isDayAscn(today, viewHolder.chooseDateView.getDayList().get(index))) {
                        if (null != chooseDataListenter) {
                            chooseDataListenter.error(1);
                            return;
                        }
                    }
                    if (null == starttime) {
                        starttime = viewHolder.chooseDateView.getDayList().get(index);
                    } else {
                        if (null != chooseDataListenter) {
                            chooseDataListenter.clean(starttime);
                            viewHolder.chooseDateView.setStartTime(starttime);
                        }
                    }
                }
                if (null != chooseDataListenter) {
                    chooseDataListenter.selected(starttime);
                }
            }
        });

//        viewHolder.chooseDateView.setOnStartDaySelectedEndDayListener(new ChooseDateView.OnStartDaySelectedEndDayListener() {
//            @Override
//            public void onClick(int index) {
//                if (-1 != index && null != viewHolder.chooseDateView && null != viewHolder.chooseDateView.getDayList() &&
//                        viewHolder.chooseDateView.getDayList().size() > 0 &&
//                        viewHolder.chooseDateView.getDayList().size() > index) {
//
//                    if (null != starttime && null == endtime) {
//                        if (!viewHolder.chooseDateView.isValid(starttime, viewHolder.chooseDateView.getDayList().get(index)))
//                            if (null != chooseDataListenter) {
//                                chooseDataListenter.error(0);
//                                return;
//                            }
//
//                    }
//                    if (viewHolder.chooseDateView.isDayAscn(today, viewHolder.chooseDateView.getDayList().get(index))) {
//                        if (null != chooseDataListenter) {
//                            chooseDataListenter.error(1);
//                            return;
//                        }
//                    }
//                    if (null == starttime) {
//                        starttime = viewHolder.chooseDateView.getDayList().get(index);
//                    } else if (null != starttime && null == endtime) {
//                        if (viewHolder.chooseDateView.isDayAsc(starttime, viewHolder.chooseDateView.getDayList().get(index))) {
//                            endtime = viewHolder.chooseDateView.getDayList().get(index);
//                            viewHolder.chooseDateView.setStartTime(starttime);
//                            viewHolder.chooseDateView.setEndTime(endtime);
//                        } else {
//                            endtime = starttime;
//                            starttime = viewHolder.chooseDateView.getDayList().get(index);
//                            viewHolder.chooseDateView.setStartTime(starttime);
//                            viewHolder.chooseDateView.setEndTime(endtime);
//                        }
//                    } else if (null != starttime && null != endtime) {
//                        starttime = viewHolder.chooseDateView.getDayList().get(index);
//                        endtime = null;
//                        viewHolder.chooseDateView.setStartTime(starttime);
//                        viewHolder.chooseDateView.setEndTime(endtime);
//                        if (null != chooseDataListenter) {
//                            chooseDataListenter.clean(starttime);
//                        }
//                    }
//                }
//                if (null != chooseDataListenter) {
//                    chooseDataListenter.selected(starttime, endtime);
//                }
//            }
//        });

        if (null != starttime) {
            String startText;
            if ((starttime.getMonth() + 1) < 10) {
                startText = starttime.getYear() + "年0" + (starttime.getMonth() + 1) + "月";
            } else {
                startText = starttime.getYear() + "年" + (starttime.getMonth() + 1) + "月";
            }
            if (viewHolder.title.getText().equals(startText)) {
                viewHolder.chooseDateView.setStartTime(starttime);
                if (null != endtime)
                    viewHolder.chooseDateView.setEndTime(endtime);
            } else {
                viewHolder.chooseDateView.setStartTime(null);
                viewHolder.chooseDateView.setEndTime(null);
            }
        }
        if (null != endtime) {
            String endText;

            if ((endtime.getMonth() + 1) < 10) {
                endText = endtime.getYear() + "年0" + (endtime.getMonth() + 1) + "月";
            } else {
                endText = endtime.getYear() + "年" + (endtime.getMonth() + 1) + "月";
            }
            if (viewHolder.title.getText().equals(endText)) {
                if (null != starttime)
                    viewHolder.chooseDateView.setStartTime(starttime);

                viewHolder.chooseDateView.setEndTime(endtime);

            }
        }
        viewHolder.chooseDateView.invalidate();
        return convertView;
    }


    public interface ChooseDataListenter {
        //有开始有结束
        void selected(Day starttime, Day endtime);

        //只有开始时间
        void selected(Day starttime);

        //清空输入
        void clean(Day starttime);

        //长度超长
        void error(int i);
    }

    ChooseDataListenter chooseDataListenter;

    public void setChooseDataListener(ChooseDataListenter chooseDataListenter) {
        this.chooseDataListenter = chooseDataListenter;

    }


    @Override
    public int getCount() {
        if (null != mData) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (null != mData && position < getCount()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        if (view instanceof PinnedHeaderListView) {
            ((PinnedHeaderListView) view).controlPinnedHeader(firstVisibleItem);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }


    @Override
    public int getPinnedHeaderState(int position) {
        if (getCount() == 0 || position < 0) {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_GONE;
        }

        if (isMove(position) == true) {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP;
        }

        return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_VISIBLE;
    }


    @Override
    public void configurePinnedHeader(View headerView, int position, int alpaha) {
        CalendarData itemEntity = (CalendarData) getItem(position);
        String headerValue = itemEntity.getTitle();

        Log.e(TAG, "header = " + headerValue);

        if (!TextUtils.isEmpty(headerValue)) {
            TextView headerTextView = (TextView) headerView.findViewById(R.id.header);
            headerTextView.setText(headerValue);
        }

    }

    private boolean needTitle(int position) {
        if (position == 0) {
            return true;
        }

        if (position < 0) {
            return false;
        }

        CalendarData currentEntity = (CalendarData) getItem(position);
        CalendarData previousEntity = (CalendarData) getItem(position - 1);
        if (null == currentEntity || null == previousEntity) {
            return false;
        }

        String currentTitle = currentEntity.getTitle();
        String previousTitle = previousEntity.getTitle();
        if (null == previousTitle || null == currentTitle) {
            return false;
        }

        if (currentTitle.equals(previousTitle)) {
            return false;
        }

        return true;
    }


    private boolean isMove(int position) {

        CalendarData currentEntity = (CalendarData) getItem(position);
        CalendarData nextEntity = (CalendarData) getItem(position + 1);
        if (null == currentEntity || null == nextEntity) {
            return false;
        }


        String currentTitle = currentEntity.getTitle();
        String nextTitle = nextEntity.getTitle();
        if (null == currentTitle || null == nextTitle) {
            return false;
        }


        if (!currentTitle.equals(nextTitle)) {
            return true;
        }

        return false;
    }

    private class ViewHolder {
        TextView title;
        private TextView firstInWeekText;
        private TextView secondInWeekText;
        private TextView thirdInWeekText;
        private TextView fourthInWeekText;
        private TextView fifthInWeekText;
        private TextView sixthInWeekText;
        private TextView seventhInWeekText;

        private ChooseDateView chooseDateView;
    }

}
